package com.app.debeziumkafka;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;

import org.modelmapper.AbstractConverter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.modelmapper.convention.MatchingStrategies;
import org.modelmapper.convention.NameTokenizers;
import org.springframework.util.StopWatch;

import com.app.common.utils.BeanUtilsX;
import com.app.common.utils.Utils;
import com.app.pojo.demo.TestResult;

//官网 http://modelmapper.org/getting-started/
public class ModelMapperSingle {

	private static final ModelMapper MM = new ModelMapper();

	static {
		// 配置 http://modelmapper.org/user-manual/configuration/
		MM.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT)
				.setSourceNameTokenizer(NameTokenizers.UNDERSCORE);
	}

	public static ModelMapper Instance() {
		return ModelMapperSingle.MM;
	}

	public static void main(String[] args) {
		ModelMapper mm = ModelMapperSingle.Instance();
		mm.addConverter(new AbstractConverter<Integer, Date>() {
			@Override
			protected Date convert(Integer source) {
				return Utils.toDate(LocalDate.ofEpochDay(source));
			}
		}, Integer.class, Date.class);

		mm.addConverter(new AbstractConverter<Long, Date>() {
			@Override
			protected Date convert(Long source) {
				LocalDateTime dt = LocalDateTime.ofInstant(Instant.ofEpochMilli(source), ZoneOffset.UTC);
				return Utils.toDate(dt);
			}
		}, Long.class, Date.class);

		mm.addConverter(new AbstractConverter<String, Date>() {
			@Override
			protected Date convert(String source) {
				return Utils.toDateFromUTC(source);
			}
		}, String.class, Date.class);

//		mm.typeMap(Map.class, TestResult.class).addMappings(mapper -> {
//			mapper.map(src -> src.get("qf"), TestResult::setQuestionFlag);
//			mapper.map(src -> src.get("a"), TestResult::setAnswer);
//		});

		// https://www.codenong.com/cs107021453/
		// 创建自定义映射规则
		var pm = new PropertyMap<Map<String, Object>, TestResult>() {
			@Override
			protected void configure() {

				map().setQuestionFlag((String) source.get("qf"));
				map().setAnswer((String) source.get("a"));

			}
		};

//		mm.addMappings(pm);

		var d = new TestResult();
		var s = Map.of("id", "1111", "TEST_DATE", 19694, "TEST_TIME", 1701616200000L, "BEGIN_TIME",
				"2021-12-02T07:19:31Z", "SCORE_REMARK", "111aaa", "question_Id", "questionId", "qf", "questionFlag",
				"a", "answer");
		StopWatch sw=new StopWatch("copy");
		sw.start("modelmap");
		mm.map(s, d);
		sw.stop();
		
		sw.start();
//		BeanUtilsX.mapToBean(d, Utils.underlineToCamel(s));
		sw.stop();

//		BeanUtils.copyProperties(s, d);

		System.out.println(d);
	}
}